#include "pch.h"

#include "iosystem/streamreader.h"



namespace IO
{

	///
	RE_ImplementClass('STRR', IO::StreamReader, Core::ReferenceCount);





	///
	StreamReader::StreamReader():
	m_isOpen(false),
		m_streamWasOpen(false)
	{

	}


	///
	StreamReader::~StreamReader()
	{

	}





	///
	void StreamReader::SetStream(const SmrtPtr<Stream> & strm)
	{
		this->m_stream = strm;
	}


	///
	const SmrtPtr<Stream> & StreamReader::GetStream() const 
	{
		return this->m_stream;
	}


	///
	bool StreamReader::ValidStream() const 
	{
		return this->m_stream.isvalid();
	}


	///
	bool StreamReader::Eof() const
	{
		return this->m_stream->Eof();
	}


	///
	bool StreamReader::IsOpen() const 
	{
		return this->m_isOpen;
	}


	///
	bool StreamReader::Open()
	{
		if(this->m_stream->IsOpen())
		{
			this->m_streamWasOpen = true;
			this->m_isOpen = true;
		}
		else
		{
			this->m_streamWasOpen = false;
			this->m_stream->SetAccessMode(Stream::ReadAccess);
			this->m_isOpen = this->m_stream->Open();
		}

		return this->m_isOpen;
	}


	///
	void StreamReader::Close()
	{
		if((!this->m_streamWasOpen) && m_stream->IsOpen())
			this->m_stream->Close();

		this->m_isOpen = false;
	}

}